package com.banksoft.cps.controller.admin;

import com.banksoft.cps.bean.AdminUser;
import com.banksoft.cps.bean.BuyCompany;
import com.banksoft.cps.bean.CompanyUser;
import com.banksoft.cps.bean.TransportCompany;
import com.banksoft.cps.bean.common.ServiceException;
import com.banksoft.cps.bean.query.BuyCompanyQuery;
import com.banksoft.cps.bean.query.CompanyQuery;
import com.banksoft.cps.bean.query.TransportCompanyQuery;
import com.banksoft.cps.constants.WebConstant;
import com.banksoft.cps.controller.ControllerConstants;
import com.banksoft.cps.service.AdminUserService;
import com.banksoft.cps.service.BuyCompanyService;
import com.banksoft.cps.service.CompanyService;
import com.banksoft.cps.service.TransportCompanyService;
import com.banksoft.cps.util.CookieUtil;
import com.banksoft.cps.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by rongzhao.zhu on 2017/12/19.
 */
@Controller
@RequestMapping("/admin/")
public class AdminMainController extends ControllerConstants {

    @Autowired
    AdminUserService adminUserService;
    @Autowired
    CompanyService companyService;
    @Autowired
    BuyCompanyService buyCompanyService;
    @Autowired
    TransportCompanyService transportCompanyService;

    @RequestMapping(value = "loginOn", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String login(String account, String password, HttpServletRequest request, HttpServletResponse response){
        AdminUser user = null;
        try{
            user =  adminUserService.login(account,password);
            CookieUtil.setLoginCookie(response,user,"");
        }catch (ServiceException e){
            return toExceptionString(e);
        }
        return toJSONString(createDataTip(user));
    }
    @RequestMapping(value = "logout", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        for(Cookie cookie :request.getCookies()){
            if(WebConstant.cookieToken.equals(cookie.getName())){
                adminUserService.logout(cookie.getValue());
            }
        }
        return toJSONString(SUCCESS);
    }
    @RequestMapping(value = "updatePassword", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String updatePassword(String oldPassword,String newPassword) throws IOException {
        AdminUser adminUser = (AdminUser) UserContext.getUser();
        try{
            adminUserService.updatePassword(adminUser.getId(),oldPassword,newPassword);
        }catch (ServiceException e){
            return toExceptionString(e);
        }
        return toJSONString(SUCCESS);
    }
    @RequestMapping(value = "statisticsCompany", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String statisticsCompany() throws IOException {
        AdminUser adminUser = (AdminUser) UserContext.getUser();
        try{
            long count = companyService.count(new CompanyQuery());
            long buyCount = buyCompanyService.count(new BuyCompanyQuery());
            long transCount = transportCompanyService.count(new TransportCompanyQuery());
            Map<String,Long> countMap =new HashMap<>();
            countMap.put("count",count);
            countMap.put("buyCount",buyCount);
            countMap.put("transCount", transCount);
            return toJSONString(createDataTip(countMap));

        }catch (Exception e){
            return toExceptionString(e);
        }

    }

    //获取登录用户信息
    @RequestMapping(value = "queryFind", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryFind() throws IOException {
        AdminUser adminUser = (AdminUser) UserContext.getUser();
        return toJSONString(createDataTip(adminUser));
    }

}
